<template>
  <div>
    <el-upload
      v-if="fileList.length == 0"
      :action="useOss ? ossUploadUrl : minioUploadUrl"
      :data="useOss ? dataObj : null"
      :class="{ hide: value }"
      :show-file-list="false"
      :before-upload="beforeUpload"
      :on-remove="handleRemove"
      :on-success="handleUploadSuccess"
      :on-preview="handlePreview"
      :limit="maxCount"
      accept=".pdf,.doc,.docx,.xls,.xlsx"
      :on-exceed="handleExceed"
    >
      <div v-loading="fileLoading"
        style="width: 96px; height: 96px; background: #eee; border-radius: 8px ; line-height: 96px;"
      >
        <i class="el-icon-plus"></i>
      </div>
    </el-upload>
    <div v-else>
      <div class="file-item" v-for="(item, idx) in fileList" :key="idx">
        {{ item.url.name }}
        <i @click="delFile(idx)" class="el-icon-error icon-close"></i>
      </div>
    </div>
  </div>
</template>
<script>
import { policy } from "@/api/oss";

export default {
  name: "multiUpload",
  props: {
    //图片属性数组
    value: Array,
    //最大上传图片数量
    maxCount: {
      type: Number,
      default: 5,
    },
    uploadType: {
      type: String,
      default: "0",
    },
    width: {
      type: String,
      default: "96px",
    },
    height: {
      type: String,
      default: "96px",
    },
    border: {
      type: String,
      default: "none",
    },
    bgcColor: {
      type: String,
      default: "#fff",
    },
  },
  data() {
    return {
      dataObj: {
        policy: "",
        signature: "",
        key: "",
        ossaccessKeyId: "",
        dir: "",
        host: "",
      },
      dialogVisible: false,
      dialogImageUrl: null,
      useOss: true, //使用oss->true;使用MinIO->false
      ossUploadUrl: "http://macro-oss.oss-cn-shenzhen.aliyuncs.com",
      minioUploadUrl: `${process.env.BASE_API}/minio/upload`,
      fileLoading: false,
    };
  },
  computed: {
    fileList() {
      let fileList = [];
      for (let i = 0; i < this.value.length; i++) {
        fileList.push({ url: this.value[i] });
      }
      return fileList;
    },
  },
  methods: {
    
    delFile(idx) {
        this.value.splice(idx, 1)
        // this.$emit('delFile', idx)
    },
    handleRemove(file, fileList) {
      this.emitInput(fileList);
    },
    handlePreview(file) {
      this.dialogVisible = true;
      this.dialogImageUrl = file.url;
    },
    beforeUpload(file) {
      let _self = this;
      this.fileLoading = true;
      if (!this.useOss) {
        //不使用oss不需要获取策略
        return true;
      }
      return new Promise((resolve, reject) => {
        policy(this.uploadType)
          .then((response) => {
            _self.dataObj.policy = response.data.policy;
            _self.dataObj.signature = response.data.signature;
            _self.dataObj.ossaccessKeyId = response.data.accessKeyId;
            let fileName = file.name.replace(/,/g, "-");
            _self.dataObj.key =  response.data.dir + new Date().getTime() + fileName 
            _self.dataObj.dir = response.data.dir;
            _self.dataObj.host = response.data.host;
            _self.ossUploadUrl = response.data.host;
            _self.dataObj.callback = response.data.callback;
            resolve(true);
          })
          .catch((err) => {
            // console.log(err)
            reject(false);
          });
      });
    },

    getFileExtension(filename) {
      return filename.slice(filename.lastIndexOf(".") + 1).toLowerCase();
    },
    handleUploadSuccess(res, file) {
      console.log("res", res);
      this.fileList.push({ name: file.name, url: res.data.filename });
      console.log("this.fileList", this.fileList);
      this.fileLoading = false;
      this.$emit("input", this.fileList);
      this.$emit("uploadFileSucces");
    },
    handleExceed(files, fileList) {
      this.$message({
        message: "最多只能上传" + this.maxCount + "张图片",
        type: "warning",
        duration: 1000,
      });
    },
  },
};
</script>
<style lang="scss" scoped>
/deep/ .el-upload-list__item {
  transition: none !important;
  -webkit-transition: none !important;
}
/deep/ .el-upload-list__item-name {
  transition: none !important;
  -webkit-transition: none !important;
}

.freePic .hide /deep/ .el-upload--picture-card {
  display: none;
}


.file-item {
    background-color: #f6f7f9;
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    width: 300px;
    height: auto;
    line-height: 1;
    -webkit-box-align: center;
    -ms-flex-align: center;
    align-items: center;
    padding: 20px;
    position: relative;
    .icon-close {
        position: absolute;
        right: -5px;
        top: -5px;
        cursor: pointer;;
    }
}
</style>
